$(document).ready(function () {
    var bookId  = xhTool.getQuery('bookId'),
        subject = unescape(xhTool.getQuery('subject')),
        inList  = $('#InList'),

        // outList = $('#OutList'),
        // DataGrid2 = new DataGridEvent(),
        // schoolIdDom = $('#schoolIds'),
        // schoolId = schoolIdDom.val(),
        // subjectIdDom = $('#subjectIds'),
        // subjectId = subjectIdDom.val(),
        // inParams = {"schoolId": schoolId, "subjectId": subjectId, "bookId": bookId, "type": "in"},
        inParams = { "bookId": bookId, "type": "in"},
        // outParams = {"schoolId": schoolId, "subjectId": subjectId, "bookId": bookId, "type": "out"},
        ids,
        names,
        logonNames;
    // schoolIdDom.select2();
    // schoolIdDom.on('change', function () {
    //     schoolId = schoolIdDom.val();
    //     inParams.schoolId = schoolId;
    //     outParams.schoolId = schoolId;
    //     subjectId = subjectIdDom.val();
    //     inParams.subjectId = subjectId;
    //     outParams.subjectId = subjectId;
    //     inList.addDataGridParams(inParams);
    //     outList.addDataGridParams(outParams);
    // });
    // subjectIdDom.select2();
    // subjectIdDom.on('change', function () {
    //     schoolId = schoolIdDom.val();
    //     inParams.schoolId = schoolId;
    //     outParams.schoolId = schoolId;
    //     subjectId = subjectIdDom.val();
    //     inParams.subjectId = subjectId;
    //     outParams.subjectId = subjectId;
    //     inList.addDataGridParams(inParams);
    //     outList.addDataGridParams(outParams);
    // });

    $('.xh-datagrid-toolbar').parseToolbar();
    inList.initDataGrid(baseUrl + 'Res/getTeacherList', {"queryParams": inParams})
        .setSelectThenEnable(['bt_out'], [], 'bs');
    // outList.initDataGrid(baseUrl + 'Res/getTeacherList', {"queryParams": outParams}, DataGrid2)
    //     .setSelectThenEnable(['bt_in'], [], 'bs', DataGrid2);

    $('#bt_list_search').click(function () {
        var key        = xhExtra.getValue($('#list_search'));
        var searchType = xhExtra.getValue($('#select_type'));
        inParams.key = key;
        inParams.searchType = searchType;
        inList.addDataGridParams(inParams);
    });

    $('#bt_in').click(function () {

        var outList = window.frames['rightFrame'].techList;
        var _rows = outList.datagrid('getSelections');
        var nameArr = [];
        $.each(_rows,function (i,row) {
            if(subject != row['iSubject']){
                nameArr.push(row['sUserName']);
            }
        });
        ids = outList.getDataGridSelectedFields('iUserId');      //获取所选行未关联教师ID
        names = outList.getDataGridSelectedFields('sUserName');
        logonNames = outList.getDataGridSelectedFields('sLogonName');

        if(nameArr.length > 0){
            window.top.swal({
                title: '你确定要关联嘛?'+nameArr.join(',')+'等人学科不符',
                showCancelButton: true,
                cancelButtonText: '取消',
                confirmButtonText: '确认',
                allowOutsideClick: true,
                showCloseButton: true,
                showLoaderOnConfirm: true,
                type: 'warning'
            }).then(function(isConfirm) {
                contactQuestion('in');
            }, function(dismiss) {

            });
        }else {
            contactQuestion('in');
        }

    });

    $('#bt_out').click(function () {
        ids = inList.getDataGridSelectedFields('id');       //获取所选行已关联教师ID
        contactQuestion('out');
    });





    //根据类型进行关联或取消关联操作
    function contactQuestion(type) {

        $.post(baseUrl + 'Res/contactTeacher', {
            ids: ids,
            names: names,
            logonNames: logonNames,
            bookId: bookId,
            type: type,
        }, function (result) {
            $('body').append(result);
            inList.dataGridReload();
            $('#bt_in, #bt_out').setButtonDisable('bs');                    //按钮变为禁用状态
        });
    }
});